package com.shop.item.atom.atomsvc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.item.ItemCommentInput;
import com.shop.common.model.result.item.ItemCommentOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.item.atom.atomsvc.IItemCommentAtomService;
import com.shop.item.atom.dao.ItemCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemCommentAtomService extends BaseService implements IItemCommentAtomService {
    @Autowired
    ItemCommentMapper itemCommentMapper;

    @Override
    public ServiceBean<Integer> insertItemComment(ItemCommentInput commentInput) {
        List<ItemCommentOutput> list = itemCommentMapper.selectList(commentInput);
        if (list != null && list.size() >= 30) {
            return resultFailed("用户已经超过最大评论次数");
        }
        int num = itemCommentMapper.insertSelective(commentInput);
        if (opSuccess(num)) {
            return resultSuccess("评论成功");
        }
        return resultFailed("评论失败");
    }

    @Override
    public ServiceBean<String> deleteItemComment(ItemCommentInput commentInput) {
        return null;
    }

    @Override
    public ServiceBean<ItemCommentOutput> selectItemComment(ItemCommentInput commentInput) {
        return null;
    }

    @Override
    public ServiceBean<List<ItemCommentOutput>> selectItemCommentList(ItemCommentInput commentInput) {
        return null;
    }

    @Override
    public ServiceBean<List<ItemCommentOutput>> selectItemCommentList4P(InputBean<ItemCommentInput> inputBean) {
        ItemCommentInput itemCommentInput = inputBean.getParam();
        PageHelper.startPage(inputBean.getIndex(), inputBean.getPageSize());
        List<ItemCommentOutput> list = itemCommentMapper.selectList(itemCommentInput);
        PageInfo<ItemCommentOutput> pageInfo = new PageInfo(list);
        if (pageInfo.getList() == null || pageInfo.getList().size() <= 0) {
            return resultFailed();
        }
        ServiceBean serviceBean = new ServiceBean(pageInfo.getList());
        serviceBean.setMaxSize(pageInfo.getTotal());
        serviceBean.setSuccess(true);
        return serviceBean;
    }

    @Override
    public ServiceBean<String> updateItemComment(ItemCommentInput commentInput) {
        return null;
    }
}
